Skip to content

Match composite actions in step-level GitHub Actions recipes - #223

Merged
timtebeek merged 2 commits into
mainfrom
tim/pin-composite-action-yml
Aug 25, 2026
Merged

Match composite actions in step-level GitHub Actions recipes#223
timtebeek merged 2 commits into
mainfrom
tim/pin-composite-action-yml

Conversation

@timtebeek

Copy link
Copy Markdown
Member

Recipes like PinGitHubActionsToSha were gated on IsGitHubActionsWorkflow, so they only ran on .github/workflows/*.{yml,yaml} and silently skipped composite actions, whose steps live in action.yml under runs.steps. IsGitHubActionDefinition already matched those files and three recipes already OR'd the two preconditions together, so this extracts that into GitHubActionsPreconditions.workflowOrActionDefinition() and routes every step-level recipe through it; recipes reading workflow-only keys (on:, jobs:, runs-on:, permissions:, needs:) keep the narrower precondition.

Most matchers needed no change, since the setup-* recipes use relative ..steps[?(@.uses =~ ..)] paths and ArtifactSecurity/TemplateInjection use $..steps[*], both of which already reach runs.stepsSetupJavaCaching was the exception, anchored at $.jobs..steps, and $..steps is equivalent for workflows since nothing else there has steps. This also guards UnpinnedDockerImages against local Dockerfile builds, as a Docker container action's runs.image is often Dockerfile or ./path/Dockerfile, which has no digest to pin and would otherwise be flagged.

Tests cover one case per distinct matcher shape rather than one per recipe, plus both branches of the new Dockerfile guard; one existing test in SetupPythonUpgradePythonVersionTest asserted action.yml was skipped and now points at config.yml.

`PinGitHubActionsToSha` and friends were gated on `IsGitHubActionsWorkflow`,
so they only ever ran on `.github/workflows/*.{yml,yaml}` and silently skipped
composite actions, whose steps live in `action.yml` under `runs.steps`.

`IsGitHubActionDefinition` already matched those files, and `ChangeAction`,
`ChangeActionVersion` and `UpgradeOfficialGitHubActions` already OR'd the two
preconditions together. Extract that into
`GitHubActionsPreconditions.workflowOrActionDefinition()` and route every
step-level recipe through it, replacing the inline copies and the private
duplicate in `UpgradeOfficialGitHubActions`.

Most matchers needed no change: the `setup-*` recipes use relative
`..steps[?(@.uses =~ ..)]` paths and `ArtifactSecurity`/`TemplateInjection`
use `$..steps[*]`, both of which already reach `runs.steps`. `SetupJavaCaching`
was the exception, anchored at `$.jobs..steps`; `$..steps` is equivalent for
workflows, since nothing else there has `steps`.

Recipes that read workflow-only keys keep the narrower precondition, as
`action.yml` has no `on:`, `jobs:`, `runs-on:`, `permissions:` or `needs:`.

Also guard `UnpinnedDockerImages` against local Dockerfile builds. A Docker
container action's `runs.image` is often `Dockerfile` or `./path/Dockerfile`,
which has no digest to pin and would otherwise be flagged.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to combine, and a static call saves some char, but its the first time I've seen this pattern in our (java-based) recipes. I wonder if we should introduce this or stick with the ususal patterns..

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Aug 25, 2026
Replaces the `GitHubActionsPreconditions.workflowOrActionDefinition()` static
helper with a named recipe that ORs `IsGitHubActionsWorkflow` and
`IsGitHubActionDefinition`. A static method is only reachable from Java, so
declarative YAML recipes had no way to express "workflow or action definition"
as a precondition; a recipe can be referenced from both.

All 21 call sites now pass it to the `Preconditions.check(Recipe, ..)`
overload, which is recipe-aware, rather than handing over a bare visitor.
@timtebeek
timtebeek merged commit 5f7b1b2 into main Aug 25, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/pin-composite-action-yml branch August 25, 2026 13:11
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request recipe

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants